<?php
/**
 * Created by PhpStorm.
 * User: YanTao
 * Date: 2016/10/24
 * Time: 15:24
 */
namespace AuctionCore\SceneActivity;

class SceneActivity
{

    /**
     * 场次活动Model
     *
     * @var Model|\Think\Model
     */
    private $sceneActivityModel;

    /**
     * 活动规则Model
     *
     * @var Model|\Think\Model
     */
    private $activityRulesModel;

    /**
     * 活动编号
     *
     * @var string
     */
    private $activityId;

    /**
     * 场次活动构造函数
     *
     * SceneActivity constructor.
     * @param string $activityId 场次活动编号
     */
    function __construct($activityId = '')
    {
        $this->sceneActivityModel = M('scene_activity'); //场次活动Model
        $this->activityRulesModel = M('activity_rules'); //场次活动规则Model
        $this->activityId = $activityId;
    }

    /**
     * 活动添加。如果有规则，同时添加相应的规则
     *
     * @param $activityArr 活动数组
     * @param $rulesArr  规则数组
     * @return bool|mixed 返回活动Id
     */
    public function add($activityArr)
    {
        $activityId = $this->sceneActivityModel->add($activityArr);
        return $activityId;
    }

    /**
     * 活动编辑
     *
     * @param $activityArr 活动数组
     * @param $rulesArr 规则数组
     * @return bool
     */
    public function edite($activityArr)
    {
        //M()->startTrans();
        $result = $this->sceneActivityModel->where(['id' => $this->activityId])->save($activityArr);
        return $result;
    }

    /**
     * 删除活动
     *
     * @return bool
     */
    public function delete()
    {
        M()->startTrans();
        $map = ['id' => $this->activityId];
        $result1 = $this->sceneActivityModel->where($map)->delete();
        $result2 = $this->deleteRulesByActivityId();
        if ($result1 === false || $result2 === false) {
            M()->rollback();
            return false;
        }
        M()->commit();
        return true;
    }

    /**
     * 通过活动编号得到相应的活动信息
     *
     * @return array
     */
    public function getSceneActivityById()
    {
        $map = ['id' => $this->activityId];
        $sceneActivityArr = $this->sceneActivityModel->where($map)->select();
        $activityRulesArr = $this->getActivityRulesByActivityId();
        $arr = [
            'scene_activity_arr' => $sceneActivityArr,
            'activity_rules_arr' => $activityRulesArr,
        ];
        return $arr;
    }

    /**
     * 得到场次活动列表
     *
     * @param $page 页数
     * @param $pageSize 每页多少条
     * @param $params 搜索参数
     * @return array
     */
    public function getSceneActivityList($page, $pageSize, $params)
    {
        $page = $page < 1 ? 1 : (int)$page;
        $pageSize = $pageSize < 1 ? 10 : (int)$pageSize;
        $map = [];
        if ($params['branch_id']) {
            $map['branch_id'] = ['eq', $params['branch_id']];
        }

        if ($params['type']) {
            $map['type'] = ['eq', $params['type']];
        }

        if ($params['starttime'] && !$params['endtime']) {
            $map['starttime'] = ['egt', $params['starttime'] . " 00:00:00"];
            //$map['endtime'] = ['egt', $params['starttime'] . " 00:00:00"];
        }
        if ($params['endtime'] && !$params['starttime']) {
            //$map['starttime'] = ['elt', $params['endtime'] . " 23:59:59"];
            $map['endtime'] = ['elt', $params['endtime'] . " 23:59:59"];
        }
        if ($params['starttime'] && $params['endtime']) {
            $map['starttime'] = ['elt', $params['endtime'] . " 23:59:59"];
            $map['endtime'] = ['egt', $params['starttime'] . " 00:00:00"];
        }

        $field = 'id,name,type,branch_id,createtime,starttime,endtime,rules_type,status';
        $order = 'createtime desc';
        $total = $this->sceneActivityModel->field($field)
            ->where($map)
            ->count();

        $list = $this->sceneActivityModel->field($field)
            ->where($map)
            ->order($order)
            ->page($page)
            ->limit($pageSize)
            ->select();
        $list = $this->sortingData($list);
        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 数据整理
     *
     * @param $arr
     * @return mixed
     */
    public function sortingData($arr)
    {
        foreach ($arr as $key => $val) {
            $arr[$key]['type'] = C('ACTIVITY_TYPE.' . $val['type']);//活动类型
            $arr[$key]['type_id'] = $val['type'];//活动类型
            $branchId = $val['branch_id']; //分公司编号
            $arr[$key]['rules_type'] = C('ACTIVITY_RULES_TYPE.' . $val['rules_type']);//减免规则
            $arr[$key]['rules_type_id'] = $val['rules_type'];//规则编号
            $branchName = \AuctionCore\Account\Back\Department::getDepartById($branchId)['name'];
            $arr[$key]['branch_name'] = $branchName;
            $map['scene_activity_id'] = $val['id'];
            $sceneCount = M('scene_activity_relation')->where($map)->count();
            $arr[$key]['is_edite'] = true;
            if ($sceneCount > 0) {
                $arr[$key]['is_edite'] = false;
            }
            /*$arr[$key]['createtime'] = date('Y年m月d日',$val['createtime']);
            $arr[$key]['starttime'] = date('Y年m月d日',$val['starttime']);
            $arr[$key]['endtime'] = date('Y年m月d日',$val['endtime']);*/
        }
        return $arr;
    }

    /**
     * 活动禁用
     *
     * @return bool
     */
    public function disableActivity()
    {
        $data = [
            'status' => 0,
        ];
        $map = ['id' => $this->activityId];
        return $this->sceneActivityModel->where($map)->save($data);
    }

    /**
     * 活动开启
     *
     * @return bool
     */
    public function enableActivity()
    {
        $data = [
            'status' => 1,
        ];
        $map = ['id' => $this->activityId];
        return $this->sceneActivityModel->where($map)->save($data);
    }

    /**
     * 活动开启或者禁用
     *
     * @return bool
     */
    public function enableOrDisableActivity($status)
    {
        $data = [
            'status' => $status,
        ];
        $map = ['id' => $this->activityId];
        return $this->sceneActivityModel->where($map)->save($data);
    }

    /**
     * 根据活动编号得到活动详情
     *
     * @return mixed
     */
    public function getActivityRulesByActivityId()
    {
        $map = [
            'scene_activity_id' => $this->activityId,
        ];
        return $this->activityRulesModel->where($map)->select();
    }

    /**
     * 添加活动规则
     *
     * @param $rulesArr 规则数组
     * @return mixed
     */
    public function addActivityRules($rulesArr)
    {
        return $this->activityRulesModel->addAll($rulesArr);
    }

    /**
     * 编辑活动规则
     *
     * @param $rulesArr 规则数组
     * @return bool
     */
    public function editeActivityRules($rulesArr)
    {
        foreach ($rulesArr as $key => $val) {
            $result = $this->activityRulesModel->save($val);
            if ($result === flase) {
                return false;
            }
        }
        return true;
    }

    /**
     * 通过规则编号，删除规则
     *
     * @param $id
     * @return mixed
     */
    public function deleteActivityRulesById($id)
    {
        $map = [
            'id' => $id,
        ];
        return $this->activityRulesModel->where($map)->delete();
    }

    /**
     * 通过场次活动编号，删除规则
     *
     * @return mixed
     */
    public function deleteRulesByActivityId()
    {
        $map = [
            'scene_activity_id' => $this->activityId,
        ];
        return $this->activityRulesModel->where($map)->delete();
    }

    /**
     * 根据分公司编号得到相应的活动信息
     *
     * @return mixed
     */
    public static function getSceneActivityByBranchId($params)
    {
        $map = array();
        if ($params['branch_id'] != '0') {
            $map['branch_id'] = array('eq', $params['branch_id']);
        }
        $map['starttime'] = ['elt', $params['start_time']];
        $map['endtime'] = ['egt', $params['start_time']];
        $map['status'] = array('eq', '1');
        $fields = 'id,name,branch_id';
        $order = 'createtime desc';
        $result = M('scene_activity')->field($fields)->where($map)
            ->order($order)
            ->select();
        return $result;
    }

    /**
     * 通过活动名称和活动编号，得到有效的场次活动总数
     *
     * @param string $activityName 活动名称
     * @param string $activityId 活动编号
     * @return mixed
     */
    public static function getValidSceneActivityByName($activityName, $activityId = '')
    {
        $map['name'] = ['eq', $activityName];
        $map['endtime'] = ['egt', date('Y-m-d H:i:s', time())];
        $map['status'] = 1;
        if (!empty($activityId)) {
            $map['id'] = ['neq', $activityId];
        }
        $total = M('scene_activity')->where($map)->count();
        return $total;
    }
}